package com.thinkgem.jeesite.cn.yy.action;

import com.thinkgem.jeesite.cn.yy.entity.ProTableCol;
import com.thinkgem.jeesite.cn.yy.service.SQLsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jws24 on 2017/12/1.
 */
@Controller
@RequestMapping("SQLAction")
public class SQLAction {

    @Autowired
    SQLsService sqlsService;

    private final Logger logger = LoggerFactory.getLogger(SQLAction.class);

    @RequestMapping("sql")
    @ResponseBody
    public Map<String, Object> SQL(
                                   @RequestParam(value = "pro_id", defaultValue = "") int proId,
                                   @RequestParam(value = "tab_id", defaultValue = "") int tabId) throws IOException {
        Map<String, Object> map = new HashMap();
        FileWriter fw = new FileWriter("D:\\dbDoc.txt");
        List<Map<String, String>> proName = sqlsService.proName(proId);
        for (Map<String, String> mapString : proName) {
            for (String name : mapString.values()) {
                System.out.print("*********************************");
                System.out.println(name.toString());
                fw.write("CREATE DATABASE" + " " + name.toString() + ";" + "\r\n");
                fw.write("\r\n");
                fw.write("\r\n");
            }
        }
        List<Map<String, String>> tableName = sqlsService.tabName(tabId);
        //取表名
        for (Map<String, String> String : tableName) {
            for (String tabNameString : String.values()) {
                System.out.println("///////////////////////////////////");
                System.out.println(tabNameString);
                String str = "CREATE TABLE" + " " + tabNameString + "(" + System.getProperty("line.separator");
                fw.write(str);
                List<ProTableCol> proTableCol = sqlsService.col(tabId);
                logger.info("{}", proTableCol);
                String name="";
                for (int i = 0; i < proTableCol.size(); i++) {
                    System.out.println("--->>>" + proTableCol.get(i).getCol_name());
                    System.out.println("--->>>" + proTableCol.get(i).getCol_length());

                    if (proTableCol.get(i).isCol_isNull() == 0) {
                        String isNull = "DEFAULT NULL";
                        if (proTableCol.get(i).getCol_restrict().equals("primary")) {
                                 name +="\r\n"+ proTableCol.get(i).getCol_name() + " " + proTableCol.get(i).getCol_type() + " "
                                        + "(" + proTableCol.get(i).getCol_length() + ")" + " " + " " + isNull + " " + "PRIMARY KEY" + "," ;
                                System.out.println(name.toString());
                            }else{
                                 name+= "\r\n"+proTableCol.get(i).getCol_name() + " " + proTableCol.get(i).getCol_type() + " "
                                        + "(" + proTableCol.get(i).getCol_length() + ")" + " " + " " + isNull + " " +  ",";
                                System.out.println(name.toString());
                            }
                    } else {
                        String isNull = "NOT NULL";
                        if (proTableCol.get(i).getCol_restrict().equals("primary")) {
                                 name+= "\r\n"+proTableCol.get(i).getCol_name() + " " + proTableCol.get(i).getCol_type() + " "
                                        + "(" + proTableCol.get(i).getCol_length() + ")" + " " + " " + isNull + " " + "PRIMARY KEY" + "," ;
                                System.out.println(name.toString());
                            }else{
                                 name+= "\r\n"+proTableCol.get(i).getCol_name() + " " + proTableCol.get(i).getCol_type() + " "
                                        + "(" + proTableCol.get(i).getCol_length() + ")" + " " + " " + isNull + " " + "," ;
                                System.out.println(name.toString());
                            }

                        }

                    }
                name= name.substring(0,name.length()-1);
                fw.write(name);
                }
                fw.write("\r\n"+")" + "\r\n");
                fw.write("\r\n");
                fw.write("\r\n");
            }
        fw.close();
        map.put("message","ok");
        return map;
        }

    }



